import 'package:flutter/material.dart';
import 'package:loan_calculator/calculator.dart';

class FoundationEstimatePage extends StatefulWidget {
  const FoundationEstimatePage({super.key});

  @override
  State<FoundationEstimatePage> createState() => _FoundationEstimatePageState();
}

class _FoundationEstimatePageState extends State<FoundationEstimatePage> {
  final _baseNumberController = TextEditingController();
  final _monthController = TextEditingController();
  final _materialStatesController = MaterialStatesController();

  int amount = 0;
  _computeLoanAmount() {
    if (_checkAllFieldHasInput()) {
      final monthBaseNumber = double.parse(_baseNumberController.text);
      final mothCount = int.parse(_monthController.text);
      setState(() {
        amount = (mothCount * (mothCount + 1) / 2 * monthBaseNumber * 0.9 / 1000).floor() * 1000;
      });
    }
  }

  @override
  void initState() {
    super.initState();
    _baseNumberController.addListener(() {
      _materialStatesController.update(MaterialState.disabled, !_checkAllFieldHasInput());
    });
    _monthController.addListener(() {
      _materialStatesController.update(MaterialState.disabled, !_checkAllFieldHasInput());
    });
  }

  @override
  void dispose() {
    super.dispose();
    _baseNumberController.dispose();
    _monthController.dispose();
    _materialStatesController.dispose();
  }

  bool _checkAllFieldHasInput() {
    return _baseNumberController.text.isNotEmpty && _monthController.text.isNotEmpty;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('公积金贷款试算'),
      ),
      body: SingleChildScrollView(
        child: Padding(
          padding: const EdgeInsets.all(16),
          child: Column(
            children: [
              InputUnit(
                unit: '元',
                placeholder: '月缴存基数',
                controller: _baseNumberController,
                decimal: true,
              ),
              const SizedBox(
                height: 16,
              ),
              InputUnit(
                unit: '月',
                placeholder: '已缴存月数',
                controller: _monthController,
              ),
              const Padding(padding: EdgeInsets.only(bottom: 50.0)),
              TextButton(
                onPressed: _computeLoanAmount,
                statesController: _materialStatesController,
                style: ButtonStyle(
                    backgroundColor: MaterialStateProperty.resolveWith((states) {
                      if (states.contains(MaterialState.disabled)) {
                        return Colors.grey;
                      } else {
                        return const Color(0xff6076ff);
                      }
                    }),
                    minimumSize: const MaterialStatePropertyAll(Size(double.infinity, 50))),
                child: const Text('开始计算'),
              ),
              Padding(
                padding: const EdgeInsets.only(top: 20.0),
                child: Offstage(
                  offstage: amount == 0,
                  child: Text('可贷款:$amount元'),
                ),
              )
            ],
          ),
        ),
      ),
    );
  }
}
